// Flutter imports:

import 'package:flutter/material.dart';

// Package imports:
import 'package:date_format/date_format.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:palmf_hid/common/entities/data_base/my_database.dart';
import 'package:palmf_hid/common/values/values.dart';
import 'package:palmf_hid/pages/main/show_history_data/data_back_dialog.dart';

// Project imports:

///历史数据列表项
Widget historyItem(
    {required Results result,
    required int index,
    required Function onDelete,
    required Function onPrint,
    required Function onWifiPrint,
    bool hideOperate = false}) {
  String sampleType = 'unknown'.tr;
  switch (result.sample) {
    case 1:
      sampleType = 'wholeBlood'.tr;
      break;
    case 2:
      sampleType = 'serum'.tr;
      break;
    case 3:
      sampleType = 'other'.tr;
      break;
  }
  String resultStr = result.result.toString();
  if ((result.saveNum ?? -1) != -1) {
    resultStr = result.result.toStringAsFixed(result.saveNum);
  }

  String resultDescription = "暂无描述";

  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text('${index + 1}.'),
          Expanded(
            child: result.jointInspectionNo == "0"
                ? const SizedBox()
                : Center(
                    child: Text(
                        '${result.jointInspectionNum}${'Joint inspection'.tr} : ${result.jointInspectionNo}',
                        style: TextStyle(
                            fontSize: 12, color: Colors.red.shade200))),
          ),
          Text('HP${result.deviceNo == "" ? "000000000000" : result.deviceNo}',
              style: _TextStyle())
        ],
      ),
      // Text('UP:${result.referUpper} L:${result.referLower} S:${result.saveNum}',
      //     style: _TextStyle()),

      const SizedBox(
        height: 5,
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Expanded(
              flex: 3,
              child: Text(
                  '${'project'.tr}:${result.projectName.replaceAll(" ", "").replaceAll("\u0000", "")}',
                  style: _TextStyle())),
          Expanded(
              flex: 4,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                        child: Text('${'type'.tr}:$sampleType',
                            style: _TextStyle())),
                    Text('${'number'.tr}:${result.patientId}',
                        style: _TextStyle())
                  ]))
        ],
      ),
      const SizedBox(
        height: 5,
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          Expanded(
              flex: 3,
              child: Text(
                  '${'result'.tr}:${result.resultDisplay.contains("↓") || result.resultDisplay.contains("↑") ? "" : result.resultDisplay}$resultStr${result.resultDisplay.contains("↓") || result.resultDisplay.contains("↑") ? result.resultDisplay : ""}${result.resultUnit}',
                  style: _TextStyle())),
          Expanded(
              flex: 4,
              child: Text('结论:$resultDescription', style: _TextStyle())),
        ],
      ),
      const SizedBox(
        height: 5,
      ),
      Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Text(
            '${result.jointInspectionNum == "" ? 'synchronizeOrTestTime'.tr : "Detection time".tr}：${formatDate(result.time, [
                  yyyy,
                  '-',
                  mm,
                  '-',
                  dd,
                  " ",
                  HH,
                  ':',
                  nn,
                  ':',
                  ss
                ])}',
            style: const TextStyle(color: Colors.grey, fontSize: 12),
          )
        ],
      ),
      const SizedBox(
        height: 5,
      ),
      hideOperate
          ? const SizedBox()
          : Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Container(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        color: Colors.redAccent),
                    child: InkWell(
                      onTap: () {
                        showDialog(
                            context: Get.context!,
                            builder: (ctx) {
                              return buildDataBackDialog(
                                  str: "deleteHistoryNotice".tr,
                                  onPress: () {
                                    onDelete(result);
                                  });
                            });
                      },
                      child: Text(
                        "delete".tr,
                        style: const TextStyle(
                            color: MyColor.c_FFFFFFFF, fontSize: 12),
                      ),
                    )),
                const SizedBox(
                  width: 10,
                ),
                Container(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        color: Colors.blue),
                    child: InkWell(
                      onTap: () {
                        onWifiPrint(result);
                      },
                      child: Text(
                        "wifiPrint".tr,
                        style: const TextStyle(
                            color: MyColor.c_FFFFFFFF, fontSize: 12),
                      ),
                    )),
                const SizedBox(
                  width: 10,
                ),
                Container(
                    padding:
                        const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        color: Colors.green),
                    child: InkWell(
                      onTap: () {
                        onPrint(result);
                      },
                      child: Text(
                        "blePrint".tr,
                        style: const TextStyle(
                            color: MyColor.c_FFFFFFFF, fontSize: 12),
                      ),
                    ))
              ],
            )
    ],
  );
}

_TextStyle() {
  return const TextStyle(color: Colors.black54, fontSize: 12);
}
